import allure
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait


class BasePage:
    base_url = 'https://svr-6-9010.share.51env.net'
    # 如果换了一台电脑部署网站，域名或者ip地址可能发生变化
    # 可以把base_url放在命令行，动态指定服务器域名
    page_url = '/'

    def __init__(self, driver: WebDriver):
        self.driver = driver
        self.url = self.base_url + self.page_url

    def open(self):
        self.driver.get(self.url)

    # -> ：声明返回值类型
    def find_ele(self, by, value) -> WebElement:
        return WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(by, value))
        # return WebDriverWait(self.driver, 20, 0.5).until(expected_conditions.visibility_of_element_located((by, value)))

    @allure.step('向元素({1},{2})输入{3}')
    def send_keys(self, by, value, keys):
        element = self.find_ele(by, value)
        element.clear()
        element.send_keys(keys)

    @allure.step('点击元素({1}={2})')
    def click(self, by, value):
        self.find_ele(by, value).click()

    def assert_page_contains_test(self, text):
        assert text in self.driver.page_source
